﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using AdoNet = System.Data;
using System.Runtime.CompilerServices;

namespace DataSet4Com
{
    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.AutoDispatch)]
    [ComDefaultInterface(typeof(IDataColumnCollection))]
    [Guid("8A05349F-5951-4ACC-A5F2-8125FF132ED1")]
    public class DataColumnCollection : IDataColumnCollection
    {
        private AdoNet.DataColumnCollection _adoNetColumnCollection;

        public DataColumnCollection() : this(null) { }

        public DataColumnCollection(AdoNet.DataColumnCollection adoNetColumnCollection)
        {
            if (adoNetColumnCollection == null)
                throw new ArgumentNullException("adoNetColumnCollection");
            else
                _adoNetColumnCollection = adoNetColumnCollection;
        }

        [ComVisible(false)]
        public AdoNet.DataColumnCollection AdoNetDataColumnCollection
        {
            get { return _adoNetColumnCollection; }
        }

        [DispId(-4)]
        public IEnumerator GetEnumerator()
        {
            var result = from AdoNet.DataColumn column in _adoNetColumnCollection
                         select new DataColumn(column);

            return result.GetEnumerator();
        }

        [DispId(0)]
        [IndexerName("_Default")]
        public DataColumn this[int index]
        {
            get 
            {
                return new DataColumn(_adoNetColumnCollection[index]);
            }
        }

        public DataColumn GetColumnByName(string columnName)
        {
            return new DataColumn(_adoNetColumnCollection[columnName]);
        }

        public DataColumn Add()
        {
            return new DataColumn(_adoNetColumnCollection.Add());
        }

        public void AddColumn(DataColumn column)
        {
            _adoNetColumnCollection.Add(column.AdoNetDataColumn);
        }

        public DataColumn AddWithName(string columnName)
        {
            return new DataColumn(_adoNetColumnCollection.Add(columnName));
        }

        public DataColumn AddWithNameAndType(string columnName, string type)
        { 
            Type typeObject = Type.GetType(type);

            return new DataColumn(_adoNetColumnCollection.Add(columnName, typeObject));
        }

        public DataColumn AddWithExpression(string columnName, string type, string expression)
        {
            Type typeObject = Type.GetType(type);

            return new DataColumn(_adoNetColumnCollection.Add(columnName, typeObject, expression));
        }

        public bool CanRemove(DataColumn column)
        {
            return _adoNetColumnCollection.CanRemove(column.AdoNetDataColumn);
        }

        public void Clear()
        {
            _adoNetColumnCollection.Clear();
        }

        public bool Contains(string name)
        {
            return _adoNetColumnCollection.Contains(name);
        }

        public int IndexOfByColumn(DataColumn column)
        {
            return _adoNetColumnCollection.IndexOf(column.AdoNetDataColumn);
        }

        public int IndexOf(string columnName)
        {
            return _adoNetColumnCollection.IndexOf(columnName);
        }

        public void RemoveColumn(DataColumn column)
        {
            _adoNetColumnCollection.Remove(column.AdoNetDataColumn);
        }

        public void Remove(string name)
        {
            _adoNetColumnCollection.Remove(name);
        }

        public void RemoveAt(int index)
        {
            _adoNetColumnCollection.RemoveAt(index);
        }

        public int Count
        {
            get { return _adoNetColumnCollection.Count; }
        }
    }

    [ComVisible(true)]
    [InterfaceType(ComInterfaceType.InterfaceIsDual)]
    [Guid("5E9DDB0F-C400-4F48-8DC6-128157B49D3E")]
    public interface IDataColumnCollection
    {
        [DispId(-4)]
        IEnumerator GetEnumerator();

        [DispId(0)]
        [IndexerName("_Default")]
        DataColumn this[int index]
        {
            get;
        }

        DataColumn GetColumnByName(string columnName);

        DataColumn Add();

        void AddColumn(DataColumn column);

        DataColumn AddWithName(string columnName);

        DataColumn AddWithNameAndType(string columnName, string type);

        DataColumn AddWithExpression(string columnName, string type, string expression);

        bool CanRemove(DataColumn column);

        void Clear();

        bool Contains(string name);

        int IndexOfByColumn(DataColumn column);

        int IndexOf(string columnName);

        void RemoveColumn(DataColumn column);

        void Remove(string name);

        void RemoveAt(int index);

        int Count
        {
            get;
        }
    }
}
